<?php

namespace System\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container;
use System\Model\Utility;
use System\Model\News;
use System\Model\Actionimg;

class NewsController extends AbstractActionController {

    private function checklogin() {
        $session = new Container("ssadmin");
        $username = $session->adminusername;
        if ($username == null) {
            $this->redirect()->toUrl(WEB_PATH . "/cpadmin");
        }
    }

    private $NewsTable;

    private function getNewsTable() {
        if (!$this->NewsTable) {
            $pst = $this->getServiceLocator();
            $this->NewsTable = $pst->get("System\Model\NewsTable");
        }
        return $this->NewsTable;
    }

    public function indexAction() {

        $this->checklogin();
        $this->layout("layout/layoutadmin");
    }

    public function loadnewsAction() {
        $objul = new Utility();
        $row_per = 50;
        //$start=1;
        $start = addslashes(trim($this->params()->fromQuery("page")));
        if ($start == null) {
            $start = addslashes(trim($this->params()->fromPost("page")));
        }
        $key = addslashes(trim($this->params()->fromPost("key")));
        $listnews = $this->getNewsTable()->loadnewsmodel($row_per, $start, $key);
        $listcount = ceil($this->getNewsTable()->countloadnews($key) / $row_per);
        $view = "";
        $index = 1;
        if (count($listnews) > 0) {
            foreach ($listnews as $key => $listdata) {
                $status = '<span class="label label-success">Đang chạy</span>';
                if ($listdata["news_Active"] == false) {
                    $status = '<span class="label label-danger">Tạm ngưng</span>';
                }
                $view .='<tr id="' . $listdata["news_Id"] . '">' .
                        '<td>' . $index . '</td>' .
                        '<td>' . $listdata["news_Title"] . '</td>' .
                        '<td>' . $listdata["news_Description"] . '</td>' .
                        '<td>' . $objul->formatDate($listdata["news_Date"]) . '</td>' .
                        '<td>' . $status . '</td>' .
                        '<td>' .
                        '<a onclick="delcatalog(' . $listdata["news_Id"] . ');" style="padding:0px 5px;background: #990000;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-trash-o"></i></a>' .
                        '<a style="padding:0px 5px;background: #ff9933;color:#FFF;border-radius: 3px;" href="' . WEB_PATH . '/systemadmin/news/edit?news_id=' . $listdata["news_Id"] . '" title=""><i class="fa fa-pencil-square-o"></i></a>';
                if ($listdata["news_Active"] == false) {
                    $view.='<a onclick="setActive(' . $listdata["news_Id"] . ',1);" style="padding:0px 5px;background: #006600;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-unlock-alt"></i></a>';
                } else {
                    $view .='<a onclick="setActive(' . $listdata["news_Id"] . ',0);" style="padding:0px 5px;background: #990000;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-lock"></i></a>';
                }

                $view .='</td>' .
                        '</tr>';
                $index ++;
            }
            $view .="*duypc*";
            if ($listcount > 1) {
                for ($i = 0; $i < $listcount; $i++) {
                    $view.='<a class="pageadmin" id="page' . ($i + 1) . '" onclick="setpage(' . ($i + 1) . ');" href="javascript:void(0);">' . ($i + 1) . '</a>';
                }
            }
        } else {
            $view .="Chưa có tin tức nào.*duypc*";
        }
        echo $view;
        die;
    }

    public function addnewsAction() {
        $this->layout("layout/layoutadmin");
        $action = $this->params()->fromPost("btn");
        if ($action != null) {
            $this->actionaddnews();
        } else {
            $this->checklogin();
        }
    }

    public function geturlnewsAction() {
        $objul = new Utility();
        $title = $objul->removescript(addslashes(trim($this->params()->fromPost("titlevideos"))));
        $splitTitle = $objul->chuyenDoi($title);
        $checkurl = $this->getNewsTable()->checkurlnews($splitTitle);
        echo $checkurl;
        die;
    }

    private function actionaddnews() {
        $objUl = new Utility();
        $newsTitle = $objUl->removescript(addslashes(trim($this->params()->fromPost("titlevideos"))));
        if ($newsTitle == null || strlen($newsTitle) < 6) {
            echo "err*duypc*<br/> - Tiêu đề tin tức lớn hơn 6 kí tự - <i style='color:#F56954;'>Lỗi</i>";
            die;
        }
        $videolink = $this->getNewsTable()->checkurlnews($objUl->chuyenDoi($newsTitle));
        $newsDes = $objUl->removescript(addslashes(trim($this->params()->fromPost("desvideos"))));
        $content = stripslashes($this->params()->fromPost("content"));

        //Action upload and crop img
        $objImg = new Actionimg();
        //url hinh anh
        $url = $this->params()->fromPost("urlimg");
        $tmpimg = "";
        $filename = "";
        $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/imgNews/");
        $dirpaththumb = $dirpath . "imgthumb/";
        $fileimgnew = "";
        if ($url != null) {
            $tmpimg = $url;
            $filename = $url;
        } else {
            $tmpimg = $_FILES["imgfile"]["tmp_name"];
            $filename = $_FILES["imgfile"]["name"];
        }
        $ext = substr(strrchr($filename, '.'), 1);
        $fileimgnew = time() . "." . $ext;
        copy($tmpimg, $dirpaththumb . $fileimgnew);

        @$objImg->croppThis($dirpaththumb . $fileimgnew);
        $data = array();
        $data["news_Title"] = $newsTitle;
        $data["news_Description"] = $newsDes;
        $data["news_link"] = $videolink;
        $data["news_imgThumb"] = "imgNews/imgthumb/" . $fileimgnew;
        $data["news_Content"] = $content;
        $data["news_Date"] = Date("Y:m:d");
        $data["news_Active"] = true;
        $objNews = new News();
        $objNews->exchangeArray($data);
        $add = $this->getNewsTable()->addnews($objNews);
        if ($add) {
            echo "ok*duypc*<br/> - thêm tin tức thành công. - <i style='color:#F56954;'>OK</i>";
        } else {
            echo "err*duypc*<br/> - Máy chủ đang bận vui lòng thử lại sau. - <i style='color:#F56954;'>Lỗi</i>";
        }
        die;
    }

    public function activeAction() {
        $videosId = addslashes(trim($this->params()->fromPost("videosId")));
        $active = addslashes(trim($this->params()->fromPost("active")));
        $this->getNewsTable()->status($videosId, $active);
        die;
    }

    public function delnewsAction() {
        $newsId = addslashes(trim($this->params()->fromPost("newsId")));
        $this->getNewsTable()->delImg($newsId);
        $checkdel = $this->getNewsTable()->delnews($newsId);
        if ($checkdel) {
            echo "delok*duypc*Xóa tin tức thành công.";
        } else {
            echo "delok*duypc*Đã phát sinh ra lỗi khi thực hiện lệnh xóa.";
        }
        die;
    }

    public function editAction() {
        $this->layout("layout/layoutadmin");
        $action = $this->params()->fromPost("btn");
        if ($action != null) {
            $this->actionedit();
        } else {
            $news_Id = addslashes(trim($this->params()->fromQuery("news_id")));
            $dataedit = $this->getNewsTable()->getdataedit($news_Id);
            return array("dataedit" => $dataedit);
        }
    }

    private function actionedit() {
        $objUl = new Utility();
        $newsTitle = $objUl->removescript(addslashes(trim($this->params()->fromPost("titlevideos"))));
        if ($newsTitle == null || strlen($newsTitle) < 6) {
            echo "err*duypc*<br/> - Tiêu đề tin tức lớn hơn 6 kí tự - <i style='color:#F56954;'>Lỗi</i>";
            die;
        }
        $newslink = $this->getNewsTable()->checkurlnews($objUl->chuyenDoi($newsTitle));
        $newsId = $this->params()->fromPost("newsId");
        $newsDes = $objUl->removescript(addslashes(trim($this->params()->fromPost("desvideos"))));
        $content = stripcslashes($this->params()->fromPost("content"));
        $flagimg = $this->params()->fromPost("flagImg");
        $data = array();
        $data["news_Title"] = $newsTitle;
        $data["news_Description"] = $newsDes;
        $data["news_link"] = $newslink;

        if ($flagimg == "0") {

            //Action upload and crop img
            $this->getNewsTable()->delImg($newsId);
            $objImg = new Actionimg();
            //url hinh anh
            $url = $this->params()->fromPost("urlimg");
            $tmpimg = "";
            $filename = "";
            $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/imgNews/");
            $dirpaththumb = $dirpath . "imgthumb/";
            $fileimgnew = "";
            if ($url != null) {
                $tmpimg = $url;
                $filename = $url;
            } else {
                $tmpimg = $_FILES["imgfile"]["tmp_name"];
                $filename = $_FILES["imgfile"]["name"];
            }
            $ext = substr(strrchr($filename, '.'), 1);
            $fileimgnew = time() . "." . $ext;
            copy($tmpimg, $dirpaththumb . $fileimgnew);
            @$objImg->croppThis($dirpaththumb . $fileimgnew);
            $data["news_imgThumb"] = "imgNews/imgthumb/" . $fileimgnew;
        }
        $data["news_Content"] = $content;
        $add = $this->getNewsTable()->updatenews($data, $newsId);
        if ($add) {
            echo "ok*duypc*<br/> - Update tin tức ok. - <i style='color:#F56954;'>OK</i>";
        } else {
            echo "err*duypc*<br/> - Máy chủ đang bận vui lòng thử lại sau. - <i style='color:#F56954;'>Lỗi</i>";
        }
    }

}
